$(function () {
    var bindUrl = '/personSet/bindMail?token='+localStorage.getItem('token');
    var sendUrl = '/personSet/sendMailCode?token='+localStorage.getItem('token');
    var exg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

    //点击发送验证码到指定手机中中
    $('#getcode').click(function () {
        var mail = $('#mail').val();
        if (!mail||mail==''){
            $.toast('请输入邮箱');
        } else if (!mail.match(exg)){
            $.toast('输入的邮箱格式不正确');
        } else {
            sendUrl = sendUrl+'&mail='+mail;
            time(this);
            $.getJSON(sendUrl,function (data) {
                $.toast(data.message);
            })
        }
    });

    $('#submit').click(function () {
        var mail = $('#mail').val();
        var code = $('#mailCode').val();
        var url = bindUrl+'&mail='+mail+'&code='+code;
        $.getJSON(url,function (data) {
            $.toast(data.errMsg);
            if (data.success) {
                window.location.href = "/person/accountSecurity";
            }
        })
    });


    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttribute("disabled");
            o.value="免费获取验证码";
            wait = 60;
        } else {
            o.setAttribute("disabled", true);
            o.value=wait+"秒后可以重新发送";
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }
})
